Ensure tag_fg_color and tag_bg_color are valid hex colors.

Guilherme J. Tramontina 10 years ago
parent
commit
454f43fddc
2 changed files with 14 additions and 0 deletions
  1. 4 0
      app/models/scenario.rb
  2. 10 0
      spec/models/scenario_spec.rb

+ 4 - 0
app/models/scenario.rb

@@ -9,6 +9,10 @@ class Scenario < ActiveRecord::Base
9 9
 
10 10
   validates_presence_of :name, :user
11 11
 
12
+  validates_format_of :tag_fg_color, :tag_bg_color,
13
+    :with => /\A#(?:[0-9a-fA-F]{3}){1,2}\z/, :message => "must be a valid hex color."
14
+    # Regex adapted from: http://stackoverflow.com/a/1636354/3130625
15
+
12 16
   validate :agents_are_owned
13 17
 
14 18
   protected

+ 10 - 0
spec/models/scenario_spec.rb

@@ -29,6 +29,16 @@ describe Scenario do
29 29
       new_instance.should_not be_valid
30 30
     end
31 31
 
32
+    it "validates tag_fg_color is hex color" do
33
+      new_instance.tag_fg_color = '#N07H3X'
34
+      new_instance.should_not be_valid
35
+    end
36
+
37
+    it "validates tag_bg_color is hex color" do
38
+      new_instance.tag_bg_color = '#N07H3X'
39
+      new_instance.should_not be_valid
40
+    end
41
+
32 42
     it "only allows Agents owned by user" do
33 43
       new_instance.agent_ids = [agents(:bob_website_agent).id]
34 44
       new_instance.should be_valid